package com.ruoyi.medical.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.medical.domain.MeiomsDevice;
import com.ruoyi.medical.service.IMeiomsDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备基本信息Controller
 * 
 * @author ruoyi
 * @date 2025-12-03
 */
@RestController
@RequestMapping("/medical/device")
public class MeiomsDeviceController extends BaseController
{
    @Autowired
    private IMeiomsDeviceService meiomsDeviceService;

    /**
     * 查询设备基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('medical:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeiomsDevice meiomsDevice)
    {
        startPage();
        List<MeiomsDevice> list = meiomsDeviceService.selectMeiomsDeviceList(meiomsDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('medical:device:export')")
    @Log(title = "设备基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeiomsDevice meiomsDevice)
    {
        List<MeiomsDevice> list = meiomsDeviceService.selectMeiomsDeviceList(meiomsDevice);
        ExcelUtil<MeiomsDevice> util = new ExcelUtil<MeiomsDevice>(MeiomsDevice.class);
        util.exportExcel(response, list, "设备基本信息数据");
    }

    /**
     * 获取设备基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(meiomsDeviceService.selectMeiomsDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备基本信息
     */
    @PreAuthorize("@ss.hasPermi('medical:device:add')")
    @Log(title = "设备基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeiomsDevice meiomsDevice)
    {
        return toAjax(meiomsDeviceService.insertMeiomsDevice(meiomsDevice));
    }

    /**
     * 修改设备基本信息
     */
    @PreAuthorize("@ss.hasPermi('medical:device:edit')")
    @Log(title = "设备基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeiomsDevice meiomsDevice)
    {
        return toAjax(meiomsDeviceService.updateMeiomsDevice(meiomsDevice));
    }

    /**
     * 删除设备基本信息
     */
    @PreAuthorize("@ss.hasPermi('medical:device:remove')")
    @Log(title = "设备基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(meiomsDeviceService.deleteMeiomsDeviceByDeviceIds(deviceIds));
    }

    /**
     * 生成设备二维码
     */
    @PreAuthorize("@ss.hasPermi('medical:device:generateQrcode')")
    @Log(title = "设备基本信息", businessType = BusinessType.OTHER)
    @PostMapping("/generateQrcode/{deviceId}")
    public AjaxResult generateQrcode(@PathVariable Long deviceId)
    {
        String qrcodeUrl = meiomsDeviceService.generateDeviceQrcode(deviceId);
        return success(qrcodeUrl);
    }

    /**
     * 批量生成设备二维码
     */
    @PreAuthorize("@ss.hasPermi('medical:device:batchGenerateQrcode')")
    @Log(title = "设备基本信息", businessType = BusinessType.OTHER)
    @PostMapping("/batchGenerateQrcode")
    public AjaxResult batchGenerateQrcode(@RequestBody Long[] deviceIds)
    {
        int successCount = meiomsDeviceService.batchGenerateDeviceQrcode(deviceIds);
        return success("成功生成" + successCount + "个设备二维码");
    }
}
